﻿using System;
using System.Windows.Data;

namespace Dsl.Ped.Common.Converters
{
    /// <summary>
    /// Converts a string via a <see cref="ConvertFunc"/> given in constructor
    /// </summary>
    public class ToStringConverter : IValueConverter
    {
        public Func<object, string> ConvertFunc { get; private set; }

        public ToStringConverter(Func<object, string> convertFunc)
        {
            ConvertFunc = convertFunc;
        }

        #region Implementation of IValueConverter

        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                return this.ConvertFunc(value);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }
}
